package com.hangwei.paperless.fragment.sub;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hangwei.paperless.R;
import com.hangwei.paperless.SubRepairActivity;
import com.hangwei.paperless.adapter.sub.BatchSheetAdapter;
import com.hangwei.paperless.adapter.sub.BatchStepAdapter;
import com.hangwei.paperless.model.bean.BatchStep;
import com.hangwei.paperless.model.bean.CardToSeal;
import com.hangwei.paperless.model.bean.LotDataSheet;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.utils.JsonPostUtil;
import com.hangwei.paperless.utils.NFCCallBack;
import com.hangwei.paperless.widget.MaxRecyclerView;
import com.twelve.all.base.LoadFragment;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.NotFastClickListener;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;

import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by 张龙臣 on 2020/11/27.
 * 批量零件记录单
 */

public class SubBatchFragment extends LoadFragment {

    private BatchStepAdapter mStepAdapter;
    private BatchSheetAdapter mSheetAdapter;
    private String mBillId;
    private String mUserId;
    private SubRepairActivity mActivity;

    @Override
    protected View onRealCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mBillId = arguments.getString("billId");
            mActivity = (SubRepairActivity) getActivity();
        }
        View inflate = inflater.inflate(R.layout.fragment_sub_batch, container, false);
        TextView tvSave = inflate.findViewById(R.id.tvSave);
        MaxRecyclerView recyclerViewStep = inflate.findViewById(R.id.recyclerViewStep);
        MaxRecyclerView recyclerViewSheet = inflate.findViewById(R.id.recyclerViewSheet);
        recyclerViewStep.setLayoutManager(new LinearLayoutManager(mActivity));
        mStepAdapter = new BatchStepAdapter(null) {
            @Override
            protected void onSeal(final BatchStep item) {
                mActivity.sealByCard(item.id, new NFCCallBack() {
                    @Override
                    public void onCallBack(List<CardToSeal.StampBean> stampIds, String userId) {
                        if (stampIds==null||stampIds.size()==0){
                            ToastUtil.showCenterToast("无印章！");
                        }else {
                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                            loadingDialog.show();
                            StaticPresenter.stampLotDataSheetStep(mUserId, mActivity.getBillId(), item.id, String.valueOf(item.totalQuantity), userId, stampIds.get(0).stampId, new CallBack<Object>() {
                                @Override
                                public void onCompleted(Object o) {
                                    loadingDialog.dismiss();
                                    mActivity.closeBottomPop();
                                    refresh();
                                }

                                @Override
                                public void onError() {
                                    loadingDialog.dismiss();
                                }
                            });
                        }
                    }
                });
                //选印章
//                if (App.stamps != null && App.stamps.size() > 0) {
//                    //有印章
//                    MainBottomPopupWindow mainBottomPopupWindow = mActivity.showBottomPopupWindow(BottomPopType.Seal);
//                    mainBottomPopupWindow.setOnSealListener(new MainBottomPopupWindow.OnSealListener() {
//                        @Override
//                        public void onSeal(String stampId) {
//                            final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
//                            loadingDialog.show();
//
//                            StaticPresenter.stampLotDataSheetStep(mUserId, mActivity.getBillId(), item.id, String.valueOf(item.totalQuantity), mUserId, stampId, new CallBack<Object>() {
//                                @Override
//                                public void onCompleted(Object o) {
//                                    loadingDialog.dismiss();
//                                    mActivity.closeBottomPop();
//                                    refresh();
//                                }
//
//                                @Override
//                                public void onError() {
//                                    loadingDialog.dismiss();
//                                }
//                            });
//                        }
//                    });
//                } else {
//                    ToastUtil.showCenterToast("无印章");
////                    String signPic = SPUtil.getString(SPKey.Account.SIGN_PIC);
////                    if (TextUtils.isEmpty(signPic)) {
////
////                    } else {
////
////                    }
//                }
            }
        };
        recyclerViewStep.setAdapter(mStepAdapter);
//        recyclerViewStep.addOnItemTouchListener(new OnItemChildClickListener() {
//            @Override
//            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//
//            }
//        });
        recyclerViewSheet.setLayoutManager(new LinearLayoutManager(mActivity));
        mSheetAdapter = new BatchSheetAdapter(null);
        recyclerViewSheet.setAdapter(mSheetAdapter);
//        getLotDataSheets  不存在NA DRS 上表展开显示图片
//        saveLotDataSheet
//        stampLotDataSheetStep  盖章
        tvSave.setOnClickListener(mListener);
        mUserId = SPUtil.getString(SPKey.Account.USER_ID);
        final LoadingDialog dialog = new LoadingDialog(mActivity);
        dialog.show();
        StaticPresenter.getLotDataSheets(mUserId, mBillId, new CallBack<LotDataSheet>() {
            @Override
            public void onCompleted(LotDataSheet lotDataSheet) {
                dialog.dismiss();
                mSheetAdapter.setNewData(lotDataSheet.lotData);
                if (lotDataSheet.stepData!=null&&lotDataSheet.stepData.size()>0)
                    for (int i=0;i<lotDataSheet.stepData.size();i++) {
                        BatchStep batchStep = lotDataSheet.stepData.get(i);
                        if (TextUtils.isEmpty(batchStep.technicianId)){
                            batchStep.canSeal = true;
                            break;
                        }
                    }
                mStepAdapter.setNewData(lotDataSheet.stepData);
            }

            @Override
            public void onError() {
                dialog.dismiss();
            }
        });
        return inflate;
    }

    private NotFastClickListener mListener = new NotFastClickListener() {
        @Override
        protected void isNotFastClick(View v) {
            switch (v.getId()) {
                case R.id.tvSave:
                    final LoadingDialog loadingDialog = new LoadingDialog(getActivity());
                    loadingDialog.show();
                    String jsonData = JsonPostUtil.buildSaveLotData(mUserId, mBillId, mSheetAdapter.getData());
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData);
                    StaticPresenter.saveLotDataSheet(requestBody, new CallBack<Object>() {
                        @Override
                        public void onCompleted(Object o) {
                            loadingDialog.dismiss();
                            ToastUtil.showCenterToast("保存成功");
                        }

                        @Override
                        public void onError() {
                            loadingDialog.dismiss();
                            ToastUtil.showCenterToast("保存失败");
                        }
                    });
                    break;
            }
        }
    };
}
